﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM.Admin.Page_Admin
{
    public partial class Gadget : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadParent();
            }
        }
        protected void LoadParent()
        {
            BUS.GadgetItemsControl ctr = new BUS.GadgetItemsControl();
            cboParent.DataSource = ctr.GetParentName(cboType.SelectedValue, ref sErr);
            cboParent.DataTextField = cboParent.DataValueField = "Parent";
            cboParent.DataBind();
        }
        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }
        private void RefreshForm(string val)
        {
            txtID.Text = val;
            txtName.Text = val;
            //cboParent.Text = val;
            txtLink.Text = val;
            lbErr.Text = val;
            cboTarget.Text = val;
            txtRank.Text = "0";
            ckbDefault.Checked = false;
            ckbNew.Checked = false;
            txtDesc.Text = "";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.GadgetItemsInfo usrInf = new DTO.GadgetItemsInfo();
            usrInf = GetDataFromForm(usrInf);

            BUS.GadgetItemsControl ctr = new BUS.GadgetItemsControl();
            if (txtID.Text != "" && txtID.Text != "-1")
            {
                sErr = ctr.Update(usrInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Update successfull";
            }
            else
            {
                txtID.Text = ctr.Add(usrInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Insert successfull";
            }
            if (sErr == "")
            {
                dgvList.Rebind();
                LoadParent();
            }
        }

        private DTO.GadgetItemsInfo GetDataFromForm(DTO.GadgetItemsInfo usrInf)
        {
            if (txtID.Text != "" && txtID.Text != "-1")
                usrInf.ID = Convert.ToInt32(txtID.Text);
            usrInf.Name = txtName.Text;
            usrInf.Type = cboType.SelectedValue;
            usrInf.Rank = Convert.ToInt32(txtRank.Text);
            usrInf.Target = cboTarget.SelectedValue;
            usrInf.Parent = cboParent.Text;
            usrInf.Link = txtLink.Text;
            usrInf.IsDefault = ckbDefault.Checked ? "T" : "F";
            usrInf.IsNew = ckbNew.Checked ? "T" : "F";
            usrInf.Description = txtDesc.Text;
            return usrInf;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString();
                    BUS.GadgetItemsControl ctr = new BUS.GadgetItemsControl();
                    sErr = ctr.Delete(Convert.ToInt32(id));
                }
            }
            if (sErr == "")
            {
                dgvList.Rebind();
                LoadParent();
            }
        }

        protected void dgvList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            BUS.GadgetItemsControl ctr = new BUS.GadgetItemsControl();
            dgvList.DataSource = ctr.GetByType(cboType.SelectedValue, ref  sErr);
        }

        protected void dgvList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                BUS.GadgetItemsControl ctr = new BUS.GadgetItemsControl();
                SetDataToForm(ctr.Get(Convert.ToInt32(id), ref sErr));
                lbErr.Text = "";
            }
        }

        private void SetDataToForm(DTO.GadgetItemsInfo gadgetItemsInfo)
        {
            if (gadgetItemsInfo.ID != -1)
                txtID.Text = gadgetItemsInfo.ID.ToString();
            txtName.Text = gadgetItemsInfo.Name;
            txtLink.Text = gadgetItemsInfo.Link;
            cboParent.SelectedValue = gadgetItemsInfo.Parent;
            cboType.Text = gadgetItemsInfo.Type;
            cboTarget.SelectedValue = gadgetItemsInfo.Target;
            txtRank.Text = gadgetItemsInfo.Rank.ToString();
            ckbDefault.Checked = gadgetItemsInfo.IsDefault == "T";
            ckbNew.Checked = gadgetItemsInfo.IsNew == "T";
            txtDesc.Text = gadgetItemsInfo.Description;
        }

        protected void cboType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadParent();
            dgvList.Rebind();
            if (cboType.SelectedValue == "Testing")
            {
                lbExample.Text = "TestTemplate.aspx?cat=...&top=...&lev=... ";
            }
            else lbExample.Text = "LearnTemplate.aspx?cat=...&top=...&lev=... ";
        }
    }
}
